﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WOS.Common
{
   
    public enum ResourceStatus
    {
        Working,
        Disabled,
        Incompatible,
        Removed
    }
    public sealed class Resource
    {
        #region Private Fields
        private readonly Guid id;
        private readonly Guid provider;
        private readonly string name;
        private readonly DataSize size;
        private readonly Uri location;
        private readonly ResourceStatus status;
        #endregion
        #region Constructors
        public Resource(Guid provider, string name, DataSize size, Uri location, ResourceStatus status)
        {
            this.id = Guid.NewGuid();
            this.provider = provider;
            this.name = name;
            this.size = size;
            this.location = location;
            this.status = status;
        }

        public Resource(Guid id, Guid provider, string name, DataSize size, Uri location, ResourceStatus status)
        {
            this.id = id;
            this.provider = provider;
            this.name = name;
            this.size = size;
            this.location = location;
            this.status = status;
        }
        #endregion

        #region Public Members
        public Guid Id
        {
            get { return this.id; }
        }

        public Guid Provider
        {
            get { return this.provider; }
        }

        public string Name
        {
            get { return this.name; }
        }

        public DataSize Size
        {
            get { return this.size; }
        }

        public Uri Location
        {
            get { return this.location; }
        }

        public ResourceStatus Status
        {
            get { return this.status; }
        }
        #endregion
    }
    public sealed class ResourceEventArgs : EventArgs
    {
        #region Private Fields
        private readonly Resource resource;
        #endregion
        #region Constructors
        public ResourceEventArgs(Resource resource)
        {
            this.resource = resource;
        }
        #endregion

        #region Public Members
        public Resource Resource
        {
            get { return this.resource; }
        }
        #endregion
    }
    public sealed class ResourceQueueSpecification
    {
        #region Private Fields
        private readonly string name;
        private readonly string folder;
        private readonly OriginInfo origin;

        private bool autostart;
        #endregion
        #region Constructors
        public ResourceQueueSpecification(string name, string folder, OriginInfo origin)
        {
            this.name = name;
            this.folder = folder;
            this.origin = origin;
        }
        #endregion

        #region Public Memers
        public string Name
        {
            get { return this.name; }
        }

        public string Folder
        {
            get { return this.folder; }
        }

        public OriginInfo Origin
        {
            get { return this.origin; }
        }

        public bool Autostart
        {
            get { return this.autostart; }
            set { this.autostart = value; }
        }
        #endregion
    }
    public static class ResourceHelper
    {
        public static bool AreSimilarNames(string left, string right)
        {
            var sLeft = left.Split(new[] { '_', '.', '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var sRight = right.Split(new[] { '_', '.', '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            left = String.Join("_", sLeft);
            right = String.Join("_", sRight);

            return String.Equals(left, right, StringComparison.InvariantCultureIgnoreCase);
        }

        public static string NormalizeName(string name)
        {
            var limit = Math.Min(6, name.Length - 3);

            if (limit > 1 && name.Length - 6 > name.LastIndexOf('.') && name.Length - 6 <= name.LastIndexOf('_'))
            {
                var index = name.LastIndexOf('_');
                var result = name.Substring(0, index) + '.' + name.Substring(index + 1);

                return result;
            }


            return name;
        }
    }

}
